package com.woparker.kiosk.view.mapview.layer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.widget.ImageView;


import com.woparker.kiosk.R;
import com.woparker.kiosk.anim.AnimatorPath;
import com.woparker.kiosk.anim.IconAnimator;
import com.woparker.kiosk.bean.NodeBean;
import com.woparker.kiosk.view.mapview.MapView;

import java.util.List;

/**
 * RouteLayer
 *
 * @author: yuanxd
 */
public class RouteLayer extends MapBaseLayer {
    // 线路点集合
    private List<NodeBean> nodeList; // nodes list
    // 路线宽度
    private float routeWidth; // the width of route
    //起点图标
    private Bitmap routeStartBmp;
    //终点图标
    private Bitmap routeEndBmp;
    //画笔
    private Paint paint;
    //色块宽度
    public static float TILE_WIDTH = 5;
    //路径集合
    private Path mPath = new Path();
    //画布对象
    private Canvas mCanvas;

    //声明动画集合
    private AnimatorPath animatorPath;

    private IconAnimator iconAnimator;



    public RouteLayer(MapView mapView) {
        this(mapView, null);
    }

    public RouteLayer(MapView mapView, List<NodeBean> nodeList) {
        super(mapView);
        this.nodeList = nodeList;
        initLayer();
    }

    /**
     * 初始化图层
     */
    private void initLayer() {
        this.routeWidth = 10;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLUE);
        routeStartBmp = BitmapFactory.decodeResource(mapView.getResources(),
                R.mipmap.start_point);
        routeEndBmp = BitmapFactory.decodeResource(mapView.getResources(),
                R.mipmap.end_point);
    }


    public IconAnimator getAnimatorIcon(ImageView icon){
        iconAnimator = new IconAnimator(icon);
        return iconAnimator;
    }

    /**
     * 设置线路宽度
     * @param routeWidth
     */
    public void setRouteWidth(int routeWidth){
        this.routeWidth = routeWidth;
    }


    @Override
    public void onTouch(MotionEvent event) {

    }


    public float animatorStartX,animatorStartY;
    @Override
    public void draw(Canvas canvas, Matrix currentMatrix, float currentZoom, float
            currentRotateDegrees) {
        this.mCanvas = canvas;
        animatorPath = new AnimatorPath();
        if (isVisible && nodeList != null) {
            canvas.save();
            if (!nodeList.isEmpty()) {
                // draw route
                float[] goalStart = {TILE_WIDTH * nodeList.get(0).y + TILE_WIDTH / 2, TILE_WIDTH * nodeList.get(0).x + TILE_WIDTH / 2};
                currentMatrix.mapPoints(goalStart);
                float previousX = goalStart[0];
                float previousY = goalStart[1];
                mPath.reset();
                mPath.moveTo(previousX, previousY);


                animatorStartX = previousX-iconAnimator.icon_width/2;
                animatorStartY = previousY-iconAnimator.icon_height/2;

                animatorPath.moveTo(previousX-iconAnimator.icon_width/2,previousY-iconAnimator.icon_height/2);


                for (int i = 1; i < nodeList.size(); i++) {
                    float[] goal = {TILE_WIDTH * nodeList.get(i).y + TILE_WIDTH / 2, TILE_WIDTH * nodeList.get(i).x + TILE_WIDTH / 2};
                    currentMatrix.mapPoints(goal);
                    float nextX = goal[0];
                    float nextY = goal[1];
                    float dx = Math.abs(nextX - previousX);
                    float dy = Math.abs(nextY - previousY);
                    //两点之间的距离大于等于3时，生成贝塞尔绘制曲线
                    if (dx >= 3 || dy >= 3) {
                        //设置贝塞尔曲线的操作点为起点和终点的一半
                        float cX = (nextX + previousX) / 2;
                        float cY = (nextY + previousY) / 2;
                        mPath.quadTo(previousX, previousY, cX, cY);

                        animatorPath.secondBesselCurveTo(previousX-iconAnimator.icon_width/2, previousY-iconAnimator.icon_height/2,
                                cX-iconAnimator.icon_width/2, cY-iconAnimator.icon_height/2);
                        //第二次执行时，第一次结束调用的坐标值将作为第二次调用的初始坐标值
                        previousX = nextX;
                        previousY = nextY;
                    }
                    //最后一个点画直线
                    if (i == nodeList.size() - 1) {
                        mPath.lineTo(nextX, nextY);

                        animatorPath.lineTo(nextX-iconAnimator.icon_width/2,nextY-iconAnimator.icon_height/2);
                    }
                }
                //关闭当前轮廓，若当前的点不等于第一点，则自动连接第一个点
                paint.setStrokeWidth(routeWidth);
                canvas.drawPath(mPath, paint);


                float[] goal1 = {TILE_WIDTH * nodeList.get(0).y + TILE_WIDTH / 2,
                        TILE_WIDTH * nodeList.get(0).x + TILE_WIDTH / 2};
                float[] goal2 = {TILE_WIDTH * nodeList.get(nodeList.size() - 1).y + TILE_WIDTH / 2,
                        TILE_WIDTH * nodeList.get(nodeList.size() - 1).x + TILE_WIDTH / 2};
                currentMatrix.mapPoints(goal1);
                currentMatrix.mapPoints(goal2);
                canvas.drawBitmap(routeStartBmp,
                        goal1[0] - routeStartBmp.getWidth() / 2, goal1[1]
                                - routeStartBmp.getHeight(), paint);
                canvas.drawBitmap(routeEndBmp,
                        goal2[0] - routeEndBmp.getWidth() / 2, goal2[1]
                                - routeEndBmp.getHeight(), paint);
            }
            canvas.restore();
        }
        else{
            //清除路线
            clearRoute();
        }
    }



    public   AnimatorPath getAnimatorPath(){
        if(null == animatorPath){
            animatorPath = new AnimatorPath();
        }
        return animatorPath;
    }

    /**
     * 清除路线
     */
    public void clearRoute(){
        if(null != mPath && null != mCanvas) {
            // 画背景层
            mPath.reset();
            mCanvas.drawPath(mPath, paint);
        }
    }


    public void setNodeList(List<NodeBean> nodeList) {
        this.nodeList = nodeList;
    }
}
